//
//  AttendanceStatisticsVC.swift
//  SBSafetyApp
//
//  Created by Aimeow on 2022/11/23.
// 考勤统计

import Foundation
import UIKit

class AttendanceStatisticsVC: SubLevelViewController {
    var selDate: Date = Date()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = "考勤统计"
        navigationController?.navBarStyle(.white)
        setupUI()
        reloadData()
    }
    
    func reloadData() {
        // 考勤统计
        let day = Calendar.current.component(.day, from: selDate)
        let month = Calendar.current.component(.month, from: selDate)
        let year = Calendar.current.component(.year, from: selDate)
        
        API.getSecurityStatics(withParam: SecurityStaticsParam(day: day, month: month, year: year)) { responseModel in
            DispatchQueue.main.async {
                self.calendarView.reload(modals: responseModel.model?.statics ?? [])
                self.checkListView.buildData(models: responseModel.model?.record ?? [])
            }
        }
    }
    
    func setupUI() {
        self.view.backgroundColor = UIColor.hex("#F8F8F8")
        self.view.addSubview(scrollView)
        scrollView.addSubview(contentView)
        contentView.addSubview(calendarView)
        contentView.addSubview(checkListView)
        
        scrollView.snp.makeConstraints { make in
            make.left.right.equalTo(view)
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view)
        }
        
        contentView.snp.makeConstraints { make in
            make.edges.equalTo(scrollView)
            make.width.equalTo(ScreenWidth)
        }
        
        calendarView.snp.makeConstraints { make in
            make.left.top.right.equalTo(contentView)
        }
        
        checkListView.snp.makeConstraints { make in
            make.left.right.equalTo(contentView)
            make.top.equalTo(calendarView.snp.bottom)
            make.bottom.equalTo(contentView.snp.bottom)
        }
    }
    
    lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        return scrollView
    }()
    
    lazy var contentView: UIView = {
        let view = UIView()
        return view
    }()
    
    lazy var calendarView: BSCalendarView = {
        let view = BSCalendarView()
        view.style = .Attendance
        view.delegate = self
        return view
    }()
    
    lazy var checkListView: CheckInListView = {
        let view = CheckInListView()
        return view
    }()
}

extension AttendanceStatisticsVC: BSCalendarViewDelegate {
    func calendar(didSelectDate date: Date) {
        if !Date.isSameDay(date1: selDate, date2: date) {
            selDate = date
            reloadData()
        }
    }
    
    func calendar(didViewMonth month: Int, year: Int) {
        API.getSecurityStatics(withParam: SecurityStaticsParam(day: 01, month: month, year: year)) { responseModel in
            DispatchQueue.main.async {
                self.calendarView.reload(modals: responseModel.model?.statics ?? [])
            }
        }
    }
}

